//
//  PlayVoice.m
//  MyProject
//
//  Created by jiaming yan on 2018/3/15.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "PlayVoice.h"
#import "EMAudioPlayerUtil.h"
#import "EMCDDeviceManager.h"
#import "EMVoiceConverter.h"
#import "AFHTTPSessionManager.h"

@implementation PlayVoice

+(void )audioPlayWithUrl:(NSString *)urlStr{
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *dbPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:urlStr];
    
    if (![fileManager fileExistsAtPath:dbPath]) {
        //创建传话管理者
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:ImageURL(urlStr)]];
 
        NSURLSessionDownloadTask *download = [manager downloadTaskWithRequest:request
                                                                     progress:^(NSProgress * _Nonnull downloadProgress) {
                                                                         //下载进度
                                                                  
                                                                     }
                                                                  destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
                                                                      //保存的文件路径
                                                                      NSString *fullPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:response.suggestedFilename];
                                                                      

                                                                      
                                                                      [EMAudioPlayerUtil asyncPlayingWithPath:fullPath
                                                                                                   completion:^(NSError *error)
                                                                       {
                                                                           
                                                                       }];
                                                                      return [NSURL fileURLWithPath:fullPath];
                                                                  }
                                                            completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
                                                                //保存的文件路径
                                                                NSString *fullPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:urlStr];
     
                                                                
                                                                [EMAudioPlayerUtil asyncPlayingWithPath:fullPath
                                                                                             completion:^(NSError *error)
                                                                 {
                                                                     
                                                                 }];
                                                            }];
        
        //执行Task
        [download resume];

    }else{
        [EMAudioPlayerUtil asyncPlayingWithPath:dbPath
                                     completion:^(NSError *error)
         {
             
         }];
      
    }
    
  
    
}
@end
